package com.ruoyi.ly.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.ly.domain.Project;
import com.ruoyi.ly.domain.SpaceBan;
import com.ruoyi.ly.domain.SpaceFloor;
import com.ruoyi.ly.domain.dto.PageSpaceBanDTO;
import com.ruoyi.ly.domain.dto.SpaceBanAddDTO;
import com.ruoyi.ly.domain.dto.SpaceBanUpdateDTO;
import com.ruoyi.ly.domain.vo.*;
import com.ruoyi.ly.mapper.SpaceBanMapper;
import com.ruoyi.ly.mapper.SpaceFloorMapper;
import com.ruoyi.ly.mapper.SpaceMeetingMapper;
import com.ruoyi.ly.mapper.WorkSpaceMapper;
import com.ruoyi.ly.service.SpaceBanService;
import com.ruoyi.ly.utils.ExportData;
import com.ruoyi.ly.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.rmi.ServerException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.stream.Collectors;
import java.util.stream.Stream;



@Service
public class SpaceBanServiceImpl implements SpaceBanService {

    @Autowired
    private SpaceBanMapper spaceBanMapper;
    @Autowired
    private ExportData exportData;
    @Autowired
    private SpaceFloorMapper spaceFloorMapper;
    @Autowired
    private WorkSpaceMapper workSpaceMapper;
    @Autowired
    private SpaceMeetingMapper spaceMeetingMapper;
    //分页查询
    @Override
    public PageResult pageQuery(PageSpaceBanDTO pageSpaceBanDto) {
        PageHelper.startPage(pageSpaceBanDto.getPage(),pageSpaceBanDto.getPageSize());
        Page<PageProjectVO> page = spaceBanMapper.pageQuery(pageSpaceBanDto);
        long total = page.getTotal();
        List<PageProjectVO> records = page.getResult();
        return new PageResult(total,records);
    }

    //添加楼栋
    @Override
    public void insert(SpaceBanAddDTO spaceBanAddDTO) {
        //对楼栋名称进行重复判断
        Integer i = spaceBanMapper.selectByRepeatBanName(spaceBanAddDTO.getBanName());
        if (i>0){
            throw new ServiceException("楼栋名称重复");
        }
        SpaceBan spaceBan = new SpaceBan();
        BeanUtils.copyProperties(spaceBanAddDTO,spaceBan);
        spaceBan.setCtime(String.valueOf(LocalDateTime.now()));
        spaceBanMapper.insert(spaceBan);
    }

    //删除楼栋
    @Override
    public void delete(Integer banId) {
        spaceBanMapper.delete(banId);
    }


    //楼栋信息导出
    @Override
    public void exportData(HttpServletResponse response) {
        //调用mapper方法查询所有楼栋  返回list集合
        List<PageSpaceBanVO> pageSpaceBanVoList = spaceBanMapper.findAll();
        //定义导出文件名字 和 sheet名字
        String  fileName = "楼栋数据";
        exportData.exportDataEasyExcel(response,pageSpaceBanVoList,PageSpaceBanVO.class,fileName);

    }

    //修改楼栋信息
    @Override
    public void update(SpaceBanUpdateDTO spaceBanUpdateDTO) {
        //对楼栋名称进行重复判断
        Integer i = spaceBanMapper.selectByRepeatBanName(spaceBanUpdateDTO.getBanName());
        //查询修改前的楼栋名称
        String oldBanName = spaceBanMapper.selectById(spaceBanUpdateDTO.getBanId()).getBanName();
        //判断修改前楼栋名称和修改后楼栋名称是否一致
        boolean equals = oldBanName.equals(spaceBanUpdateDTO.getBanName());

        //修改时不能与其他楼栋名称重复
        if (equals) {
            SpaceBan spaceBan = new SpaceBan();
            BeanUtils.copyProperties(spaceBanUpdateDTO,spaceBan);
            spaceBanMapper.update(spaceBan);
        }
        else{
            if (i > 0) {
                throw new ServiceException("楼栋名称重复");
            }
            SpaceBan spaceBan = new SpaceBan();
            BeanUtils.copyProperties(spaceBanUpdateDTO,spaceBan);
            spaceBanMapper.update(spaceBan);
        }

    }

    //根据id查询楼栋信息
    @Override
    public SpaceBanUpdateVO selectById(Long banId) {
        SpaceBanUpdateVO spaceBanUpdateVO = spaceBanMapper.selectById(banId);
        return spaceBanUpdateVO;
    }

    /**
     * 查询选择巡检区域
     * @param
     * @return  List<InspectionTreeVO>
     */
    @Override
    public List<InspectionTreeVO> selectInspection(String examineCateType) {

        List<InspectionTreeVO>  inspectionTreeVOList = new ArrayList<>();

        // 查询所有楼栋id  放入List<InspectionTreeVO>里面 如果examineCateType=2 则继续查询所有楼层和工作区
        List<SpaceBan> spaceBanList = spaceBanMapper.selectAllBan();
        if(examineCateType.equals("2"))
        {
            spaceBanList.forEach(spaceBan ->{
                InspectionTreeVO inspectionTreeVO = new InspectionTreeVO();
                inspectionTreeVO.setName(spaceBan.getBanName());
                inspectionTreeVO.setId(spaceBan.getBanId());
                inspectionTreeVOList.add(inspectionTreeVO);
            });
        }
        else if(examineCateType.equals("1"))
        {
            spaceBanList.forEach(spaceBan ->{
                InspectionTreeVO inspectionTreeVO = new InspectionTreeVO();
                inspectionTreeVO.setName(spaceBan.getBanName());
                inspectionTreeVO.setId(spaceBan.getBanId());
                // 查询所有楼层
                List<SpaceFloor> FloorNumberList = spaceFloorMapper.selectAllFloor(spaceBan.getBanId());
                List<WorkSpaceTreeVO> workSpaceTreeVOList = new ArrayList<>();

                    FloorNumberList.forEach(floor ->{

                        //楼层对应的楼栋号  相同的楼层在不同的楼栋  用楼栋号加以区分
                        Long banId = spaceBan.getBanId();

                        WorkSpaceTreeVO workSpaceTreeVO = new WorkSpaceTreeVO();
                        String spaceFloorName = "第"+floor.getFloorNumber()+"层";
                        workSpaceTreeVO.setName(spaceFloorName);
                        workSpaceTreeVO.setId(floor.getFloorId());

                        // 查询所有工作区和对应的工作区id
                        List<TreeChildrenVO> workSpaceNameList = workSpaceMapper.selectAllWorkSpace(floor.getFloorNumber(),banId);


                        // 查询所有功能场地和对应的功能场地id
                        List<TreeChildrenVO> spaceMeetingNameList = spaceMeetingMapper.selectAllSpaceMeeting(floor.getFloorNumber(),banId);

                        //将工作区和功能场地合并为一个集合
                        List<TreeChildrenVO> collect = Stream.of(workSpaceNameList, spaceMeetingNameList).flatMap(Collection::stream).collect(Collectors.toList());

                        workSpaceTreeVO.setChildren(collect);

                        workSpaceTreeVOList.add(workSpaceTreeVO);

                    });
                    inspectionTreeVO.setChildren(workSpaceTreeVOList);
                    inspectionTreeVOList.add(inspectionTreeVO);


            });
        }
        return inspectionTreeVOList;
    }


}
